
const shapeMap = {
    'rect': generateRectPoints,
    'triangle': generateTrianglePoints,
}
function getTextInfo(text, width, height, fontSize, space, lineHeight, overflow) {
    let len = text.length;
    let textHeight = 0;
    let row = 0;
    let x = -space;
    let y = 0;
    let w = 0;
    let h = 0;
    lineHeight = Math.max(fontSize[1], lineHeight);
    let textInfo = [];
    for (let i = 0; i < len; i++) {
        if (text.charCodeAt(i) > 255) {
            if(x + w/2 + space + fontSize[0] > width) {//超出宽度，新增一行
                row++;
                x = -space;
                y = 0;
                w = 0;
                h = 0;
            }
            x += (w/2 + space + fontSize[0]/2);
            y = -lineHeight/2 - row * (lineHeight);
            w = fontSize[0];
            h = fontSize[1];
        } else {
            if(x + w/2 + space + fontSize[0]/2 > width) {//超出宽度，新增一行
                row++;
                x = -space;
                y = 0;
                w = 0;
                h = 0;
            }
            x += (w/2 + space + fontSize[0]/2/2);
            y = -lineHeight/2 - row * (lineHeight);
            w = fontSize[0] / 2;
            h = fontSize[1];
        }
        if(height !== 'auto' && lineHeight * (row + 1) > height) {//超出给定文字区域最大高度
            if(overflow === 'hidden') {
                break;
            } else if(overflow === 'ellipsis') {
                let last3Letters = textInfo.slice(-3).reverse();
                last3Letters.forEach(item => {
                    if(width - item.x  < 3 * fontSize[0]/2) {
                        textInfo.pop()
                    }
                })
                x = textInfo[textInfo.length - 1].x + textInfo[textInfo.length - 1].w/2 + fontSize[0]/4;
                y = textInfo[textInfo.length - 1].y;
                w = fontSize[0]/2;
                h = fontSize[1];
                textInfo.push({ x, y, w, h, letter: '.' });
                textInfo.push({ x: x + fontSize[0]/2, y, w, h, letter: '.' });
                textInfo.push({ x: x + fontSize[0], y, w, h, letter: '.' });
                break;
            }
        }
        textInfo.push({ x, y, w, h, letter: text[i] });
    }
    textHeight = height !== 'auto' ? lineHeight * row : height;
    return {
        width, height: textHeight, textInfo
    }

}
function getLocationOffset(containerWidth, containerHeight, horizontalLocation, verticalLocation) {
    let offsetX = 0;
    let offsetY = 0;
    switch(horizontalLocation) {
        case 'left':  
            offsetX = -containerWidth/2;
            break;
        case 'center':  
            offsetX = 0;
            break;
        case 'right':  
            offsetX = containerWidth/2;
            break;
        default:
            offsetX = 0;
    }
    switch(verticalLocation) {
        case 'bottom': 
            offsetY = containerHeight/2; 
            break;
        case 'middle':  
            offsetY = 0;
            break;
        case 'top':  
            offsetY = -containerHeight/2;
            break;
        default:
            offsetY = 0
    }
    return [offsetX, offsetY];
}
/**
 * @class
 * @memberof w.parser
 * @param {*} message 消息
 * @param {*} callback 处理完成的回调
 */
function point(message,callback){
    
    let { points, color,horizontalLocation, verticalLocation, size, shape} = message;
    let [width, height] = size;
    let locationOffset = getLocationOffset(width, height, horizontalLocation, verticalLocation);
    let {
        positions,
        indices_array,
        directions,
        colors,
        uvs
    } = shapeMap[shape](points, color);
    positions = new Float32Array(positions);
    indices_array = new Uint32Array(indices_array);
    directions = new Float32Array(directions);
    colors = new Uint8Array(colors);
    uvs = new Float32Array(uvs);
    locationOffset = new Float32Array(locationOffset);
    callback({data: {
        positions,
        indices_array,
        directions,
        colors,
        uvs,
        locationOffset
    }, transferlist:[
        positions.buffer,
        indices_array.buffer,
        directions.buffer,
        colors.buffer,
        uvs.buffer,
        locationOffset.buffer
    ]});
}
function generateRectPoints(points, color) {
    let positions = [];
    let indices_array = [];
    let directions = [];
    let uvs = [];
    let colors = [];
    points.forEach((position, index) => {
        positions.push(...position,...position,...position,...position);
        directions.push(
            -1,1,0,
            1,1,0,
            1,-1,0,
            -1,-1,0
        );
        indices_array.push(index*4, index*4 + 2, index*4 + 1, index*4, index*4 +3, index*4 +2);
        uvs.push(0,0,1,0,1,1,0,1);
        if(color.length) {
            if(color[4*index] !== undefined && color[4*index+1] !== undefined && color[4*index+2] !== undefined && color[4*index+3] !== undefined) {
                colors.push(
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3],
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3],
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3],
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3]
                );
            } else {
                colors.push(
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1],
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1],
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1],
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1]
                );
            }
        }
    })
    return {
        positions,
        indices_array,
        directions,
        colors,
        uvs
    }
}
function generateTrianglePoints(points, color) {
    let positions = [];
    let indices_array = [];
    let directions = [];
    let uvs = [];
    let colors = [];
    points.forEach((position, index) => {
        positions.push(...position,...position,...position);
        directions.push(
            0,1,0,
            1,-1,0,
            -1,-1,0
        );
        indices_array.push(index*3, index*3 + 2, index*3 + 1);
        uvs.push(1/2,1,1,0,0,0);
        if(color.length) {
            if(color[4*index] !== undefined && color[4*index+1] !== undefined && color[4*index+2] !== undefined && color[4*index+3] !== undefined) {
                colors.push(
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3],
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3],
                    color[4*index], color[4*index+1], color[4*index+2], color[4*index+3]
                );
            } else {
                colors.push(
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1],
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1],
                    colors[colors.length-4], colors[colors.length-3], colors[colors.length-2], colors[colors.length-1]
                );
            }
        }
    })
    return {
        positions,
        indices_array,
        directions,
        colors,
        uvs
    }
}


export {point}